using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Admin2024.Domain.Interface;
using Admin2024.Domain.Entity;
using Microsoft.EntityFrameworkCore;
namespace Admin2024.EntityFrameworkCore.Repository
{
    public class EFRepository<T> : IRepository<T> where T : BaseEntity
    {
        private readonly Admin2024DbContext _db;
        private readonly DbSet<T> _tb;

        public IQueryable<T> Table {get;}

        public EFRepository(Admin2024DbContext admin2024DbContext){
            _db = admin2024DbContext;
            _tb = _db.Set<T>();
            Table = _tb.AsNoTracking();
        }

        public async Task<T> CreateAsync(T entity)
        {
            await _tb.AddAsync(entity);
            await _db.SaveChangesAsync();
            return entity;
        }

        

        public async Task<T> DeleteAsync(T entity)
        {
            //软删除
            entity.IsDeleted = true;
            return await UpdateAsync(entity);
        }

        public async Task<T?> DeleteAsync(Guid id)
        {
            var entity = await Table.SingleOrDefaultAsync(x => x.Id == id);
            if(entity == null){
                return null;
            }
            // 虽然 Remove 是同步方法，但 EF Core 允许你异步执行 SaveChangesAsync 来提交更改到数据库。
            _tb.Remove(entity);
            await _db.SaveChangesAsync();

            return entity;
        }

        public async Task<T> GetByIdAsync(Guid id)
        {

           var res =  await Table.SingleOrDefaultAsync(x => x.Id == id);
            


           return res;
        }

        public async Task<List<T>> GetListAsync()
        {
            return await Table.ToListAsync();
        }

        public Task<T> UpdateAsync(Guid id, T entity)
        {

            throw(new NotImplementedException());   

        }

        public async Task<T> UpdateAsync(T entity)
        {
             _tb.Update(entity);

            await  _db.SaveChangesAsync();

            return entity;
        }

        public async Task<T?> EnableAsync(Guid id)
        {
            var entity = await GetByIdAsync(id);
            // 如果有找到，而且其状态为禁用，则可以启用,否则返回null
            if (entity != null && entity.IsActived != true)
            {
                entity.IsActived = true;
                await UpdateAsync(entity);
                return entity;
            }
            return null;
        }

        public async Task<T?> DisableAsync(Guid id)
        {
            var entity = await GetByIdAsync(id);
            // 如果有找到，而且其状态为启用，则可以禁用,否则返回null
            if (entity != null && entity.IsActived == true)
            {
                entity.IsActived = false;
                await UpdateAsync(entity);
                return entity;
            }
            return null;
        }

        public async Task DeleteRangeAsync(IEnumerable<Guid> ids)
        {
            var entities = await _tb.Where(x => ids.Contains(x.Id)).ToListAsync();
            _tb.RemoveRange(entities);
            await _db.SaveChangesAsync();
        }
    }
}